package fr.playsoft.psas.parcel;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ParcelSample extends ListActivity {
    
	public static final String[] NAMES = {
		"Parcel <simple Object (int, String, bool)>",
		"Array of Parcel <simple Object (int, String, bool)>",
		"Parcel <String & ArrayList of Simple Obj>",
		"Array of Parcel <String & ArrayList of Simple Obj>"
	};
	
	/** Called when the activity is first created. */
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
		this.setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, NAMES));
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Intent intent = null;
		switch (position) {
		case 0:
			intent = new Intent(this, Parcel1.class);
			
			SimpleObj item = new SimpleObj();
			item.id = 13;
			item.name = "anticafe";
			item.isSimple = true;
			
			intent.putExtra("item", item);			
			break;
		case 1:
			intent = new Intent(this, Parcel2.class);
			
			ArrayList<SimpleObj> items = new ArrayList<SimpleObj>();
			for(int i = 0; i < 5; i++) {
				SimpleObj _item = new SimpleObj();
				_item.id = i;
				_item.name = "anticafe" + i;
				_item.isSimple = (i%2 == 0);
				items.add(_item);
			}			
			intent.putExtra("items", items);			
			break;
		case 2:
			intent = new Intent(this, Parcel3.class);
			
			ArrayList<SimpleObj> _items = new ArrayList<SimpleObj>();
			for(int i = 0; i < 5; i++) {
				SimpleObj _item = new SimpleObj();
				_item.id = i;
				_item.name = "anticafe" + i;
				_item.isSimple = (i%2 == 0);
				_items.add(_item);
			}
			ComplexObj1 compObj = new ComplexObj1();
			compObj.title = "Complex Object item";
			compObj.items = _items;
			
			intent.putExtra("_compObj", compObj);			
			break;
		case 3:
			intent = new Intent(this, Parcel4.class);
			
			String[] prefix = new String[] { "Obj1 ", "Obj2 " };
			ArrayList<ComplexObj1> compList = new ArrayList<ComplexObj1>();
			
			for (int j = 0; j < prefix.length; j++) {
				ArrayList<SimpleObj> _items1 = new ArrayList<SimpleObj>();
				for(int i = 0; i < 5; i++) {
					SimpleObj _item = new SimpleObj();
					_item.id = i;
					_item.name = "anticafe" + i;
					_item.isSimple = (i%2 == 0);
					_items1.add(_item);
				}				
				ComplexObj1 _compObj = new ComplexObj1();
				_compObj.title = "Complex Object item " + prefix[j];
				_compObj.items = _items1;
				compList.add(_compObj);
			}			
			intent.putParcelableArrayListExtra("_compList", compList);
			break;
		default:
			break;
		}
		if(intent != null)
			startActivity(intent);
	}
}